var appInstance = getApp();

var pageData    = {
  data: {
    keyword:'',
    unreadMsgCount: false,
    SignIn:false,
    imgUrls: [{'imgpath':'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg','link':'','sort':2},{'imgpath':'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg','link':'','sort':1},{'imgpath':'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg','link':'','sort':3}],
    shareToday:{imgPath:"http://oh4xneden.bkt.clouddn.com/tmp_1191812987o6zAJs1cHTCyKqdVf28s9WnUD35k1490172417451.jpg",
linkId:"2",linkURL:"index?id=2"},
    kefudisplay:true,  
    navbar: [{'m':'全部','id':'0'}, {'m':'行业','id':'0'}, {'m':'城市','id':'0'}],  
    currentTab: 0, 
    getProdCatlg:[],
    getProdCatlg_value:0,
    free_vessel4:{"content":[{"content":"http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg",'product_name':'exlm','company_name':'company_name',"eventParams":"{\"inner_page_link\":\"\\\/pages\\\/page10002\\\/page10002\",\"is_redirect\":0}","eventHandler":"tapInnerLinkHandler"},],
    'pageNo':0
    }
  },
  app_title: '石雕小程序',
  app_description: '石雕小程序',
  page_router: 'page10000',
  onShareAppMessage: function(){
    var pageRouter = this.page_router;
    return {
      title: this.app_title || '产品设计',
      desc: this.app_description || '',
      path: '/pages/'+pageRouter+'/'+pageRouter
    }
  },
  onLoad: function (e) {
    var that =this;
    if(e.vendorUserId){
      appInstance.globalData.vendorUserId = e.vendorUserId;
    }
    appInstance.setPageUserInfo();  
    if(appInstance.globalData.firstlogin==false){
       this.searchPrdList();
       this.requestList();
    }
  },
   onShow: function(){
    if(appInstance.globalData.page10000_inital==true){
       this.searchPrdList();
    }
  },
  onReady:function(){
     
  },
  navbarTap: function(e){
    var id = e.currentTarget.dataset.idx;
    if(id=='mymessage'){
       wx.navigateTo({
        url: '../mymessage/mymessage',
        success: function(res){
          // success
        }      
      })
      return false;
    }
    var navbar = this.data.navbar;
    this.setData({  
      currentTab: id  
    })
    if(id == 2){
      wx.navigateTo({
        url: '../switchcity/switchcity',
        success: function(res){
          // success
        }      
      })
    }
    if(id == 1){
      wx.navigateTo({
        url: '../pagehyxz/pagehyxz?returnpage=page10000',
        success: function(res){
          // success
        }      
      })
    }
  },
  choose_item:function(e){
    var that = this;
    var navbar = this.data.navbar;
    console.log(e);
    var idx = e.currentTarget;
    var idx_name = idx.dataset.idx.m;
    var idx_id = idx.dataset.idx.id;
    if(idx.id == 'city'){
      navbar[2] = {'m':idx_name,'id':idx_id};
    }else{
      navbar[1] = {'m':idx_name,'id':idx_id};
    }
    that.setData({
      navbar:navbar,
      currentTab:0
    })
  },
  downfresh:function(){
    var free_vessel4 = this.data.free_vessel4;
    free_vessel4.pageNo = free_vessel4.pageNo+1;
    this.setData({
      free_vessel4:free_vessel4
    })
    //this.searchPrdList();
    },
  searchPrdList:function(){
    var that = this;
    var pageNo = this.data.free_vessel4.pageNo;
    var param = appInstance.globalData.session_key;
    var keyword = this.data.keyword;
    var id_s = {
      id1:'',
      id2:''
    };
    try {
     var prodcatlg = wx.getStorageSync('prodcatlg')
          if (prodcatlg) {
            var navbar = that.data.navbar;
            var navbar_1;
            if(prodcatlg.subCatlgId!=''){
              navbar_1 = {
                'm':prodcatlg.subCatlg,
                'id':prodcatlg.subCatlgId
              }
            }else{
              navbar_1 = {
                'm':BizCatlgId.subCatlg,
                'id':BizCatlgId.subCatlgId
              }
            }
            id_s.id1 = navbar_1.id;
            navbar[1] = navbar_1;
            that.setData({
              navbar:navbar
            })
          }
          var city = wx.getStorageSync('city')
          if (city) {
            var navbar = that.data.navbar;
            navbar[2] = city;
            that.setData({
              navbar:navbar
            })
            id_s.id2 = city.id;
          }
        } catch (e) {
          // Do something when catch error
    }
    var searchPrdList ={
      token :param.myXcxSessionId,
      source:'1',
      target : '1',  
      userId:param.userId,
      vendorUserId:'',
      //行业分类 bizCatlg 行业细类 subCatlg 城市 cityId 关键字 keyword 
      bizCatlg:'',
      subCatlg:id_s.id1||'',
      cityId:id_s.id2||'',
      keyword:keyword||'', 
      pageNo:pageNo,
      pageSize:'',    
    }
    appInstance.sendRequest({
      url:'/prod/searchPrdList',
      data:searchPrdList,
      method:'POST',
      success: function(res){
        var data = res.userdata,
            pageN = res.page;
        var arr = []; 
        if(res.records===0){
          appInstance.showToast({
              title: '没有找到产品',
              icon: 'success',
              duration:2500
            });
            }
        for(var i=0,len=data.length;i<len;i++){
          var dataset = {}
          var coverImg = data[i].coverImg  
          dataset.content= coverImg;
          // dataset.product_name=data[i].productName;
          dataset.company_name=data[i].productName;
          dataset.eventParams=[data[i].productId,data[0].companyId];
          dataset.eventHandler='tapInnerLinkHandler'
          arr.push(dataset);
        }                  
        that.setData({
           free_vessel4:{"content":arr,'pageNo':pageN} 
        })
      },
      fail: function(res){
        console.log('getmode fail');
      },
      complete: function(res){
      }
    })
  },
  searchinput:function(e){
    var keyword = e.detail.value;
    this.setData({
      keyword:keyword
    })
  },
  requestList:function(){
    var param = appInstance.globalData.session_key;
    return appInstance.requestList(param);
  },
  bind_picker_Change:function(e){
    //bug 隐藏的时候会重置清零使用wxif解决
    var that = this;
    var getProdCatlg_value = e.detail.value;
    console.log(getProdCatlg_value)
    if(getProdCatlg_value[0]==0&&getProdCatlg_value[1]==0){return;}
    var getProdCatlg = this.data.getProdCatlg; 
    var navbar = this.data.navbar;
    navbar[1] = {
      'm':getProdCatlg[getProdCatlg_value[0]].subdata[getProdCatlg_value[1]].name,
      'id':getProdCatlg[getProdCatlg_value[0]].subdata[getProdCatlg_value[1]].id
    }
    that.setData({
      getProdCatlg_value:getProdCatlg_value[0],
      navbar:navbar
    })
    wx.setStorage({
      key: 'prodcatlg',
      data: navbar[1]
    }) 
  },
  unshow_shade:function(){
    console.log('unshow_shade')
    var that = this;
    that.setData({
      currentTab:0
    })   
    that.searchPrdList();
  },
  kefu:function(){
    var kefudisplay = this.data.kefudisplay;
     this.setData({
      kefudisplay:!kefudisplay
    })
  },
  content_call:function(e){
    console.log(e)
    return appInstance.makePhoneCall(e)
  },
  userSignIn:function(){
    var that = this;
    var param = appInstance.globalData.session_key;
    var userSign ={
      token :param.myXcxSessionId,
      source:'1', 
      userId:param.userId 
    }
     appInstance.sendRequest({
      url:'/cust/userSignIn',
      data:userSign,
      method:'POST',
      success: function(res){
        if(res.result==1){
          that.setData({
            SignIn:true
          })
          appInstance.showToast({
            title: '签到成功',
            icon: 'success'
          });
        }else if(res.result==-2){
           appInstance.showToast({
            title: res.msg||'',
            icon: 'success'
           });
           that.setData({
            SignIn:true
          })
        }  
      },
      fail: function(res){
        console.log('getmode fail');
      },
      complete: function(res){
      }
    }) 
  },
  tapInnerLinkHandler:function(e){
    var tapeven = e.currentTarget.dataset.eventParams;
    var prodId = tapeven[0]
    var param = appInstance.globalData.session_key;
    var userId = param.userId
    console.log(tapeven);
    if(tapeven!=null){
      wx.navigateTo({
        url: '../productdetail/productdetail?isSplot=false&prodId='+prodId,
        success: function(res){
          // success
        },
      })
    }
  },
  banner_event:function(e){
    var tapeven = e.currentTarget.dataset.param;
    console.log(tapeven);
    if(tapeven!=null){
      wx.navigateTo({
        url: '../contentdetail/contentdetail?targetId='+tapeven[1]+'&targetType='+tapeven[0],
        success: function(res){
          // success
        },
      })
    }
  }
};
Page(pageData);